<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 文件处理类
 */
class St_File{


    //用法：
    // copy("feiy","feiy2",1):拷贝feiy下的文件到 feiy2,包括子目录
    // copy("feiy","feiy2",0):拷贝feiy下的文件到 feiy2,不包括子目录
    //参数说明：
    // $source:源目录名
    // $destination:目的目录名
    // $iscopychild:复制时，是不是包含的子目录
    //返回结果：Array['success']拷贝是否成功；Array['errormsg']拷贝失败原因
    public static function copy($source, $destination, $iscopychild)
    {
        if (!is_dir($source))
        {
            return array('success' => false, 'errormsg' => "$source 不是一个目录");
        }

        if (!is_dir($destination) && !mkdir($destination, 0777, true))
        {
            return array('success' => false, 'errormsg' => "创建目录 $destination 失败");
        }

        $handle = opendir($source);
        if ($handle == false)
        {
            return array('success' => false, 'errormsg' => "打开目录 $source 失败");
        }

        $result = array('success' => true, 'errormsg' => "");
        while ($entry = readdir($handle))
        {
            if (($entry != ".") && ($entry != ".."))
            {
                $fromentry = $source . "/" . $entry;
                $toentry = $destination . "/" . $entry;

                if (is_dir($fromentry))
                {
                    if ($iscopychild)
                    {
                        $subDirCopyResult = self::copy($fromentry, $toentry, $iscopychild);
                        if (!$subDirCopyResult['success'])
                        {
                            $result = $subDirCopyResult;
                            break;
                        }
                    }
                }
                else
                {
                    if (!copy($fromentry, $toentry))
                    {
                        $result = array('success' => false, 'errormsg' => "拷贝文件 $fromentry 到 $toentry 失败");
                        break;
                    }
                }
            }
        }
        closedir($handle);
        return $result;
    }


    /**
     * 导出EXCEL
     * @param $rows 遍历数组
     * @param $title 第一行标题
     * @param $td_width 每一格的宽度
     * @param $col_style 每一格的样式
     *  string	@
    integer	0
    date	YYYY-MM-DD
    datetime	YYYY-MM-DD HH:MM:SS
    price	#,##0.00
    dollar	[$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00
    euro    #,##0.00 [$€-407];[RED]-#,##0.00 [$€-407]
     */
    public static function export_excel($rows,$title,$td_width,$col_style)
    {
        // include_once("../Services/XLSXWriter.php");
        include_once (BASEPATH . '/res/vendor/XLSXWriter.php');

        $filename = date('YmdHis').".xlsx";
        header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        $writer = new XLSXWriter();
        $writer->writeSheetHeader('Sheet1', $col_style, array('suppress_row'=>true,'widths'=>$td_width,'freeze_rows'=>1) );
        $format = array('font'=>'Arial','font-size'=>10,'border'=>'top,bottom', 'halign'=>'center');
        $writer->writeSheetRow('Sheet1', $title,$format);
        foreach($rows as $row)
        {
            $writer->writeSheetRow('Sheet1', $row,$format);
        }
        $writer->writeToStdOut();
        exit(0);

    }


    

}